package com.whfc.iot.mach.codec;

import com.whfc.common.util.MathUtil;
import com.whfc.iot.mach.entity.MachMsg0x0A;
import com.whfc.iot.util.MsgDataCodecUtil;
import io.netty.buffer.ByteBuf;

import java.util.Date;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/7/17 9:32
 */
public class MachMsgDataCodec0x0A implements MachMsgDataCodec<MachMsg0x0A> {

    @Override
    public void decode(ByteBuf buf, MachMsg0x0A msg) {

        //时间
        Date time = MsgDataCodecUtil.readDateTime(buf);

        //压力
        //float pressure = buf.readFloat();

        //高度
        float fPos = buf.readFloat();
        int oilPos = (int) (fPos * 1000);
        double pressure = MathUtil.round(oilPos * 9.8 / 845, 6);

        //温度
        float temp = buf.readFloat();

        msg.setTime(time);
        msg.setPressure(pressure);
        msg.setOilPos(oilPos);
        msg.setTemp(MathUtil.float2Double(temp));
    }

    @Override
    public void encode(ByteBuf buf, MachMsg0x0A msg) {

        //时间
        MsgDataCodecUtil.writeDateTime(buf, msg.getTime());

        //压力
        //buf.writeFloat(MathUtil.double2Float(msg.getPressure()));

        //高度
        buf.writeFloat(msg.getOilPos() / 1000F);

        //温度
        buf.writeFloat(MathUtil.double2Float(msg.getTemp()));
    }
}
